﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AirlineSystemApp
{
    public partial class frmScheduledFlight : Form
    {
        private ScheduleFlightDirector director;
        private ScheduleFlightBuilder builder;
        public frmScheduledFlight()
        {
            InitializeComponent();
        }


        private void frmScheduledFlight_Load(object sender, EventArgs e)
        {
            tsMessage.Text = "This Form use Builder Pattern";
            dtpFlightDate.Value = DateTime.Now;
            LoadDirector(0);
        }

        private void LoadDirector(int p)
        {
            director = new ScheduleFlightDirector();
            builder = new ScheduleFlightConcrete();
            builder.ScheduleFlightProduct.Index = p;
            director.Construct(builder);
            //Show Data
            builder.ScheduleFlightProduct.Show();
            bsScheduledFlight = builder.ScheduleFlightProduct.BsScheduledFlight;
            bnScheduledFlight.BindingSource = bsScheduledFlight;
            dgvScheduledFlight.DataSource = bsScheduledFlight;
            dgvScheduledFlight.AllowUserToAddRows = false;
            bsScheduledFlight.Position = p;
            txtFlightNo.Text = builder.ScheduleFlightProduct.txtFlightNo.Text;
            dtpFlightDate.Value = builder.ScheduleFlightProduct.DtpFlightDate.Value;
            txtFirstClass.Text = builder.ScheduleFlightProduct.txtFirstClassSeat.Text;
            txtBusinessClass.Text = builder.ScheduleFlightProduct.txtBusinessClassSeat.Text;
            txtEconomyClass.Text = builder.ScheduleFlightProduct.txtEconomyClassSeat.Text;
        }

        private void dgvScheduledFlight_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadDirector(bsScheduledFlight.IndexOf(bsScheduledFlight.Current as ScheduledFlight));
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            LoadDirector(bsScheduledFlight.IndexOf(bsScheduledFlight.Current as ScheduledFlight));
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            LoadDirector(bsScheduledFlight.IndexOf(bsScheduledFlight.Current as ScheduledFlight));
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            LoadDirector(bsScheduledFlight.IndexOf(bsScheduledFlight.Current as ScheduledFlight));
        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            LoadDirector(bsScheduledFlight.IndexOf(bsScheduledFlight.Current as ScheduledFlight));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ScheduledFlight sch = new ScheduledFlight();
                sch.FlightNo = txtFlightNo.Text;
                sch.FlightDate = dtpFlightDate.Value.Date;
                sch.FirstClassSeatsAvailable = int.Parse(txtFirstClass.Text);
                sch.BusinessClassSeatsAvailable = int.Parse(txtBusinessClass.Text);
                sch.EconomyClassSeatsAvailable = int.Parse(txtEconomyClass.Text);
                if (sch.UpdateScheduleFlight())
                {
                    MessageBox.Show("Update Success");
                    LoadDirector(bsScheduledFlight.IndexOf(bsScheduledFlight.Current as ScheduledFlight));
                }
                else
                    MessageBox.Show("Update Fail");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Update Fail due to "+ex.ToString ());
            }
        }
    }
}
